/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.bean;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import org.opu.vcforms.bean.db.ConfigPath;
import org.opu.vcforms.bean.db.DataInjector;
import org.opu.vcforms.bean.model.ComboBoxModel;
import org.opu.vcforms.bean.model.DoubleListModel;
import org.opu.vcforms.bean.model.Facultie;
import org.opu.vcforms.bean.model.ModelAvailableSource;
import org.opu.vcforms.datasource.DbManager;
import org.opu.vcforms.datasource.UserData;
import org.opu.vcforms.io.FileIO;
import org.opu.vcforms.io.Logger;

/**
 *  
 * @author yura
 */
public class ApplicationData {

    /** Data source */
    private ModelAvailableSource source;

    /** Current user data */
    private UserData currentUserData;
    /** DbManager for getting user data */
    private DbManager dbManager;

    /** Data for getting templates from db */
    private UserData templateUserData;
    /** DbManager form getting templates from db */
    private DbManager templateDbManager;

    public ApplicationData() {
        source = new ModelAvailableSource();
        ConfigPath conf = ConfigPath.getInstance();

        UserData user = null;
        try {
            user = new UserData(FileIO.load(conf.getUserDbProp(), null));
        } catch (IOException ex) {
            Logger.warning(getClass().getName() + ": can load user data:" + ex);
        }

        UserData tmplt = null;
        try {
            tmplt = new UserData(FileIO.load(conf.getTemplateDbProp(), null));
        } catch (IOException ex) {
            Logger.warning(getClass().getName() + ": can load template data:" + ex);
        }

        try {
            setCurrentUserData(user);
            setTemplateUserData(tmplt);
        } catch (ClassNotFoundException ex) {//it`s never hapen
        } catch (SQLException ex) {//it`s never hapen
        }
    }

    public ApplicationData(UserData currentUserData, UserData templateUserData) 
            throws ClassNotFoundException, SQLException {
        source = new ModelAvailableSource();
        setCurrentUserData(currentUserData);
        setTemplateUserData(templateUserData);
    }

    public UserData getCurrentUserData() {
        return currentUserData;
    }

    public final void setCurrentUserData(UserData currentUserData)
            throws ClassNotFoundException, SQLException {
        this.currentUserData = currentUserData;
        if (currentUserData != null) {
            setDbManager(new DbManager(currentUserData));
        } else {
            setDbManager(null);
        }
        reinitData();
    }

    protected void reinitData() {
        DataInjector di = new DataInjector(dbManager);
        ComboBoxModel<Integer> s = di.getSemestrs();
        ComboBoxModel<Integer> c = di.getCourses();
        ComboBoxModel<Facultie> f = di.getFaculties();
        DoubleListModel g = di.getGroups(f.getSelectedItem(), c.getSelectedItem());

        Logger.infor(String.format("[INFO]: The data was reinitialized from <%s"
                + "-semestr %s-course %s %s> to <%s-semestr %s-course %s %s>",
                source.getSemestrs().toString(), source.getCourses().toString(),
                source.getFaculties().toString(),
                source.getGroups().getAvailableGroup().toString(), s.toString(),
                c.toString(), f.toString(), g.getAvailableGroup().toString()));

        source.setSemestrs(s);
        source.setCourses(c);
        source.setFaculties(f);
        source.setGroups(g);
        source.setTemplates(new DataInjector(templateDbManager).getTemplates());
    }

    public UserData getTemplateUserData() {
        return templateUserData;
    }

    public final void setTemplateUserData(UserData templateUserData)
            throws ClassNotFoundException, SQLException {
        this.templateUserData = templateUserData;
        if (templateUserData != null){
            setTemplateDbManager(new DbManager(templateUserData));
        } else {
            setTemplateDbManager(null);
        }
        reinitTemplate();
    }

    protected void reinitTemplate(){
        DataInjector di = new DataInjector(templateDbManager);
        ComboBoxModel t = di.getTemplates();
        source.setTemplates(t);
    }

    public DbManager getDbManager() {
        return dbManager;
    }

    protected void setDbManager(DbManager dbManager) {
        this.dbManager = dbManager;
    }

    public ModelAvailableSource getSource() {
        return source;
    }

    protected void setSource(ModelAvailableSource source) {
        this.source = source;
    }

    public Template getTemplate() {
        return source.getTemplates().getSelectedItem();
    }

    public DbManager getTemplateDbManager() {
        return templateDbManager;
    }

    protected  void setTemplateDbManager(DbManager templateDbManager) {
        this.templateDbManager = templateDbManager;
    }

    public ActionListener getDataChangeListener(){
        return new ActionListenerImpl();
    }

    class ActionListenerImpl implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            DataInjector di = new DataInjector(dbManager);
            Facultie f = source.getFaculties().getSelectedItem();
            Integer c = source.getCourses().getSelectedItem();

            DoubleListModel g = di.getGroups(f, c);

            Logger.infor(String.format("Group value change invoked by <%s %d-"
                    + "course>, from %s to %s", f.toString(), c.intValue(),
                    source.getGroups().getAvailableGroup().toString(),
                    g.getAvailableGroup().toString()));

            source.setGroups(g);
        }
    }

}
